1245B - Restricted RPS - CodeForces Solution


constructive algorithms dp greedy *1200

Please click on ads to support us..

Python Code:

import math
for i in range(int(input())):
    n = int(input())
    r ,p , S = map(int , input().split())
    s = input()
    ans = []
    cnt = 0
    for j in range(n):
        if s[j]=="R":
            if p>0:
                ans.append("P")
                p-=1
                cnt+=1
            else:
                ans.append(" ")
        elif s[j]=="P":
            if S>0:
                ans.append("S")
                S-=1
                cnt+=1
            else:
                ans.append(" ")
        elif s[j]=="S":
            if r>0:
                ans.append("R")
                r-=1
                cnt+=1
            else:
                ans.append(" ")
        
        for j in range(len(ans)):
        if ans[j] == " ":
            if r>0:
                ans[j] = "R"
                r-=1
            elif p>0:
                ans[j] = "P"
                p-=1
            elif S>0:
                ans[j] = "S"
                S-=1
    if cnt >= (n+1)//2:
        print("YES")
                for j in range(n):
            print(ans[j] , end="")
        print()
    else:
        print("NO")

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,count=0,r,p,s,flag=0;
        cin>>n>>r>>p>>s;
        string s1;
        cin>>s1;
        int x=(n/2)+(n%2);
        int arr[n];
        memset(arr,0,sizeof(arr));
         for(int i = 0; i < n; i++)
        {
            if(s1[i] == 'R')
            {
                if(p)
                    count++, p--, arr[i] = 2;
            }

            else if(s1[i] == 'P')
            {
                if(s)
                    count++, s--, arr[i] = 3;
            }

            else
            {
                if(r)
                    count++, r--, arr[i] = 1;
            }
        }
       if(count >= x)
        {
            cout<<"YES"<<endl;
            for(int i = 0; i < n; i++)
            {
                if(!arr[i])
                {
                    if(r)
                        arr[i] = 1, r--;
                    else if(p)
                        arr[i] = 2, p--;
                    else
                        arr[i] = 3, s--;
                }

                if(arr[i] == 1)
                    cout<<"R";
                else if(arr[i] == 2)
                    cout<<"P";
                else
                    cout<<"S";
            }
            cout<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
}


Comments

Submit
0 Comments
More Questions

1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know